package com.edusite

class CorreosController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
    if (session.user.tipo != "admin")
    	redirect(uri:"/")
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [correosInstanceList: Correos.list(params), correosInstanceTotal: Correos.count()]
    }
    def listAlumno = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
	params.sort="fechaenv"
        params.order="desc"
        [correosInstanceList: Correos.findAllByDestinatario(session.user,params), correosInstanceTotal: Correos.count()]
    }

    def create = {
        def correosInstance = new Correos()
        correosInstance.properties = params
        correosInstance.fechaenv = new Date()
        return [correosInstance: correosInstance]
    }
    def createAlumno = {
        def correosInstance = new Correos()
        correosInstance.properties = params
        correosInstance.fechaenv = new Date()
        return [correosInstance: correosInstance]
    }
    def save = {
        def correosInstance = new Correos(params)
        correosInstance.fechaenv = new Date()
        if (correosInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'correos.label', default: 'Correos'), correosInstance.id])}"
            redirect(action: "show", id: correosInstance.id)
        }
        else {
            render(view: "create", model: [correosInstance: correosInstance])
        }
    }
        def saveAlumno = {
        def correosInstance = new Correos(params)
        correosInstance.fechaenv = new Date()
        if (correosInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'correos.label', default: 'Correos'), correosInstance.id])}"
            redirect(action: "showAlumno", id: correosInstance.id)
        }
        else {
            render(view: "createAlumno", model: [correosInstance: correosInstance])
        }
    }

    def show = {
    if (session.user.tipo != "admin")
    	redirect(action:"showPublic")
        def correosInstance = Correos.get(params.id)
        if (!correosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'correos.label', default: 'Correos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [correosInstance: correosInstance]
        }
    }
    
        def showAlumno = {
        if (!session.user)
           redirect(uri:"/")
           
        def correosInstance = Correos.get(params.id)
        
        if (!correosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'correos.label', default: 'Correos'), params.id])}"
            redirect(action: "list")
        }
        else {
        	if (correosInstance.estado=="solicitada"){
        	def respuesta = new Correos()
        	respuesta.enviadopor=session.user
        	respuesta.asunto= "Correo Recibido"
        	respuesta.texto= "Su mensaje con asunto: <strong>"+ correosInstance.asunto + " </strong> ha sido recibido por <strong>" + correosInstance.destinatario + "</strong>"
        	respuesta.fechaenv= new Date()
        	respuesta.destinatario=correosInstance.enviadopor
        	respuesta.estado="enviado"
        	respuesta.save(flush:true)
        	correosInstance.estado="recibido"

        	}
        	else if (correosInstance.estado=="requerida")
        	correosInstance.estado="solicitada"
        	else if (correosInstance.estado=="enviado")
            	correosInstance.estado="recibido"
		else if (correosInstance.estado=="enviar")
            	correosInstance.estado="enviado"
            correosInstance.save(flush:true)
            [correosInstance: correosInstance]
         }
    }

    def edit = {
        def correosInstance = Correos.get(params.id)
        if (!correosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'correos.label', default: 'Correos'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [correosInstance: correosInstance]
        }
    }

    def update = {
        def correosInstance = Correos.get(params.id)
        if (correosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (correosInstance.version > version) {
                    
                    correosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'correos.label', default: 'Correos')] as Object[], "Another user has updated this Correos while you were editing")
                    render(view: "edit", model: [correosInstance: correosInstance])
                    return
                }
            }
            correosInstance.properties = params
            if (!correosInstance.hasErrors() && correosInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'correos.label', default: 'Correos'), correosInstance.id])}"
                redirect(action: "show", id: correosInstance.id)
            }
            else {
                render(view: "edit", model: [correosInstance: correosInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'correos.label', default: 'Correos'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def correosInstance = Correos.get(params.id)
        if (correosInstance) {
            try {
                correosInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'correos.label', default: 'Correos'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'correos.label', default: 'Correos'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'correos.label', default: 'Correos'), params.id])}"
            redirect(action: "list")
        }
    }
}
